<?php 
namespace app\common\controller;

use think\Controller;

class Wechat extends Controller
{
	private static function accessToken()
	{
		$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxfe8a528cd8fc968d&secret=6003847226b40fa38f032d143d64ce35";
		$res = json_decode(Curls::curl($url),true);
		return $res['access_token'];
	}
	public static function toUserMsg($openid,$content)
	{
		
		$access_token =  self::accessToken();
		//给用户公众号推送消息
		$json = [
				"touser" => $openid,
				"msgtype" => "text",
				"text" => [
						"content" => $content
				]
		];
		$json = json_encode($json,JSON_UNESCAPED_UNICODE);
		$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$access_token}";
		Curls::http_post_json($url, $json);
	}
	
	/**
	 * 发送模板消息
	 * @param unknown $openid
	 * @param unknown $template_id
	 * @param unknown $data
	 */
	public static function modelMsg($openid,$template_id,$data,$url = "http://www.wlfxqf.com")
	{
		$access_token =  self::accessToken();
		$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
		$data = [
				'touser' => $openid,
				'template_id' => $template_id,
				"url" => $url,
				"topcolor" =>"#FF0000",
				"data" => $data,
		];
		$data_json  = json_encode($data);
		Curls::http_post_json($url, $data_json);
	}
}